package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.MammonCongratulateTime;
import com.yanqu.road.entity.decoration.UserMammon;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMammonDaoImpl extends TempDataObjectDao<UserMammon> {

    public UserMammon getUserMammon(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_mammon where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMammon data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserMammon data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_mammon(userId,worshipTime,mammonSkillId,effectTimes,totalWorshipTimes,resetTime,congratulateData) " +
                "values(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getWorshipTime());
            call.setInt(i++, data.getMammonSkillId());
            call.setInt(i++, data.getEffectTimes());
            call.setLong(i++, data.getTotalWorshipTimes());
            call.setLong(i++, data.getResetTime());
            call.setString(i++, data.getCongratulateTimeMapJson());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserMammon data) {
        PreparedStatement call = null;
        String sql = "update t_u_mammon set worshipTime=?,mammonSkillId=?,effectTimes=?,totalWorshipTimes=?,resetTime=?,congratulateData=? " +
                "where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getWorshipTime());
            call.setInt(i++, data.getMammonSkillId());
            call.setInt(i++, data.getEffectTimes());
            call.setLong(i++, data.getTotalWorshipTimes());
            call.setLong(i++, data.getResetTime());
            call.setString(i++, data.getCongratulateTimeMapJson());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserMammon parseData(ResultSet rs) throws SQLException {
        UserMammon data = new UserMammon();
        data.setUserId(rs.getLong("userId"));
        data.setWorshipTime(rs.getLong("worshipTime"));
        data.setMammonSkillId(rs.getInt("mammonSkillId"));
        data.setEffectTimes(rs.getInt("effectTimes"));
        data.setTotalWorshipTimes(rs.getLong("totalWorshipTimes"));
        data.setResetTime(rs.getLong("resetTime"));
        String congratulateData = rs.getString("congratulateData");
        Map<Integer, MammonCongratulateTime> congratulateTimeMap;
        if(StringUtils.isNullOrEmpty(congratulateData)){
            congratulateTimeMap = new ConcurrentHashMap<>();
        }else {
            congratulateTimeMap = JSON.parseObject(congratulateData, new TypeReference<Map<Integer, MammonCongratulateTime>>() {});
        }
        data.setCongratulateTimeMap(congratulateTimeMap);
        return data;
    }
}
